package com.sfhk.service.impl;

import com.sfhk.dao.UserDao;
import com.sfhk.dao.impl.UserDaoImpl;
import com.sfhk.domain.User;
import com.sfhk.service.UserService;
import com.sfhk.utils.LoginException;
import org.springframework.dao.EmptyResultDataAccessException;


public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public User login(String username, String password) throws LoginException {
        try {
            User loginUser = userDao.findByUserName(username);
            if (loginUser.getPassword().equals(password)){
                return loginUser;
            }else {
                throw new LoginException("密码错误");
            }
        }catch (EmptyResultDataAccessException e){
            throw new LoginException("用户名错误");
        }
    }
    @Override
    public void addUser(User user) {
        userDao.addUser(user);
    }

}
